package com.example.zwh.computershop.order;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Order;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.ArrayList;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    private ArrayList<Order> orderList;
    Myapp myapp;
    public OrderAdapter(ArrayList<Order> orderList, Myapp myapp) {
        this.orderList = orderList;
        this.myapp = myapp;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_list_view, viewGroup, false);

        return new ViewHolder(view);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_photo;
        TextView order_name;
        TextView order_price;
        TextView order_number;
        TextView all_price;
        Button order_btn;
        public ViewHolder (View view) {
            super(view);
            item_photo = view.findViewById(R.id.item_photo);
            order_name = view.findViewById(R.id.order_item_name);
            order_price = view.findViewById(R.id.order_item_price);
            order_number = view.findViewById(R.id.order_item_number);
            all_price = view.findViewById(R.id.all_item_price);
            order_btn = view.findViewById(R.id.order_btn);
        }
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Order or = orderList.get(i);
        Product p = new Product();
        findPro(or.getProductId(), p);
        System.out.println(or.getProductId() + " " + p.getImgUrl() + " " + p.getPrice() + " " + or.getProductNum());
        viewHolder.item_photo.setImageResource(handleData.getResourceByReflect(p.getImgUrl()));
        viewHolder.order_name.setText(p.getProductName()+p.getProductInfor());
        viewHolder.order_price.setText("价格: "+String.valueOf(p.getPrice()));
        viewHolder.order_number.setText("数量: "+String.valueOf(or.getProductNum()));
        viewHolder.all_price.setText("总价: "+String.valueOf(p.getPrice()*or.getProductNum()));
        if (or.getPayState() == 0) {
            viewHolder.order_btn.setText("待收货");
            viewHolder.order_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "待收货", Toast.LENGTH_SHORT).show();
                }
            });
        }else if (or.getPayState() == 1 && or.getReceiveState() == 1) {
            viewHolder.order_btn.setText("确认支付");
            viewHolder.order_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "确认支付", Toast.LENGTH_SHORT).show();
                }
            });
        }else if (or.getPayState() == 1 && or.getReceiveState() == 0) {
            viewHolder.order_btn.setText("评价");
            viewHolder.order_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "评价", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public void findPro(String pid, Product p) {
        int len = myapp.getProducts().size();
        for (int i = 0; i < len; i++) {
            if (pid.compareTo(myapp.getProducts().get(i).getProductId()) == 0) {
                copyPro(p, myapp.getProducts().get(i));
                break;
            }
        }
    }

    public void copyPro(Product pos, Product temp) {
        pos.setPrice(temp.getPrice());
        pos.setProductInfor(temp.getProductInfor());
        pos.setProductName(temp.getProductName());
        pos.setImgUrl(temp.getImgUrl());
        pos.setBrand(temp.getBrand());
        pos.setMark(temp.getMark());
        pos.setStockNum(temp.getStockNum());
    }
}
